﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolMgr :Sington<PoolMgr> {



    public Dictionary<string, Pool> DicPool=new Dictionary<string, Pool>();
    /// <summary>
    /// 
    /// </summary>
    /// <param name="prefabs"></param>
    /// <param name="parent"></param>
    /// <param name="init">如果为-1，则会预先创建默认数目的物体，否则创建数目为init</param>
    public void CreatePool(GameObject prefabs,Transform parent=null, int init =0)
    {

        if (!DicPool.ContainsKey(prefabs.name))
        {
            Pool p = new Pool(prefabs, parent,init);
            DicPool.Add(prefabs.name,p);
        }
    }

    /// <summary>
    /// 根据Prefab路径来创建Pool
    /// </summary>
    /// <param name="prefabName"></param>
    /// <param name="parent">需要赋值</param>
    /// <param name="init">如果为-1，则会预先创建默认数目的物体，否则创建数目为init</param>
    public void CreatePool(string  prefabName, Transform parent , int init = 0)
    {
        //prefabName =   prefabName;
        GameObject prefabs = Resources.Load(prefabName) as GameObject;
        if (!DicPool.ContainsKey(prefabs.name))
        {
            Pool p = new Pool(prefabs, parent, init);
            DicPool.Add(prefabs.name, p);
        }
    }
    /// <summary>
    /// 根据物体名字自动回收物体
    /// </summary>
    /// <param name="go">要回收的物体</param>
    public void Restore(GameObject go)
    {
        foreach (var name in DicPool.Keys)
        {
            if (go.name.StartsWith(name))
            {
                DicPool[name].Restore(go);
                break;
            }
        }

        
    }
    

    public GameObject Get(string name)
    {
            return DicPool.ContainsKey(name) ? DicPool[name].Get() : null;
    }

    public void DelePool(string name)
    {
        if (DicPool.ContainsKey(name))
        {
            DicPool.Remove(name);

        }
    }
}

public class Pool
{

    List<GameObject> pool = new List<GameObject>();
    Transform parent;
    public GameObject PrefabObject;
    public int InitCount = 5;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="prefab">预制体</param>
    /// <param name="p">父级物体</param>
    public Pool(GameObject prefab, Transform p = null, int init = -1)
    {
        PrefabObject = prefab;
        if (p != null)
            parent = p;
        else
            parent = PrefabObject.transform.parent;
        if (init != -1)
            InitCount = init;
        Init();
    }
    public void Init()
    {
        for (int i = 0; i < InitCount; i++)
        {
            pool.Add(GameObject.Instantiate(PrefabObject));
            if (parent)
                pool[i].transform.SetParent(parent, false);
            pool[i].SetActive(false);
        }
    }
    public GameObject Get()
    {
        GameObject tmp = null;
        if (pool.Count > 0)
        {
            tmp = pool[0];
            pool.RemoveAt(0);

            if (tmp == null)
                tmp = Get();
            if (parent != null)
                tmp.transform.SetParent(parent, false);
        }
        else
        {
            tmp = GameObject.Instantiate(PrefabObject);
            tmp.transform.SetParent(parent, false);
        }

        tmp.SetActive(true);
        return tmp;
    }

    public void Restore(GameObject go)
    {
        if (go == null)
            return;
        pool.Add(go);
        go.SetActive(false);
    }


}
